<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>编辑餐桌</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

  </head>
  
  <body>
    <div class="x-body">
        <form class="layui-form">
            <input type="hidden" id="tableId" name="tableId" value="${editTable.tableId}">
            <input type="hidden" id="tableTypeInp" name="tableTypeInp" value="${editTable.tableType}">
            <div class="layui-form-item">
                <label for="tableName" class="layui-form-label">
                    <span class="x-red">*</span>餐桌名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="tableName" maxlength="10" value="${editTable.tableName}" name="tableName" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>餐桌类型
                </label>
                <div class="layui-input-inline">
                    <select name="tableType">
                        <option value="大厅"  ${editTable.tableType == "大厅" ? 'selected' : ''}>大厅</option>
                        <option value="卡座"  ${editTable.tableType == "卡座" ? 'selected' : ''}>卡座</option>
                        <option value="雅间"  ${editTable.tableType == "雅间" ? 'selected' : ''}>雅间</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="tableCode" class="layui-form-label">
                    <span class="x-red">*</span>餐桌序号
                </label>
                <div class="layui-input-inline">
                    <input type="number" id="tableCode" name="tableCode" required="" value="${editTable.tableCode}" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="tableCapacity" class="layui-form-label">
                    <span class="x-red">*</span>餐桌容量
                </label>
                <div class="layui-input-inline">
                    <input type="number" id="tableCapacity" name="tableCapacity" required="" value="${editTable.tableCapacity}" lay-verify="capacity"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
          <div class="layui-form-item">
              <label  class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="edit" lay-submit="">
                  确认编辑
              </button>
          </div>
      </form>
    </div>
    <script>
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
            var form = layui.form
            ,layer = layui.layer;

            //自定义验证规则
            form.verify({
                capacity:function(value){
                    if(value <= 0){
                        return '容量必须为正数';
                    }
                },
                selectTableType:function(value){
                    if(value == 0){
                        return '请选择餐桌类型！';
                    }
                }
            });

            //输入框的值改变时触发
            $("#tableCode").on("input",function(e){
                //获取input输入的值
                if ($("select").val() ==="大厅"){
                    $('#tableName').val("大厅"+$('#tableCode').val()+"号桌");
                }else if ($("select").val()==="雅间"){
                    $('#tableName').val($('#tableCode').val()+"号雅间");
                }else if ($("select").val()==="卡座"){
                    $('#tableName').val("卡座"+$('#tableCode').val()+"号桌");
                }
            });

            //监听提交
            form.on('submit(edit)', function(data){
                //发异步，把数据提交给java
                $.ajax({
                    type:"post",
                    url:"${pageContext.request.contextPath}/table?m=edit",
                    data: data.field,
                    dataType:"json",
                    success:function (data) {
                        layer.msg(data.msg, function () {
                            if (data.code === 200){
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        });

                    }
                });
            return false;
          });

            // 获取分类数据
            $.ajax({
                type:"post",
                url:"${pageContext.request.contextPath}/food?m=getAllFoodType",
                dataType:"json",
                success:function (data) {
                    if (data.code === 200){
                        $("select[name=foodTypeCode]").empty();
                        $(data.data).each(function () {
                            if ($("#foodTypeCodeInp").val() === this.foodTypeCode1){
                                $("select[name=foodTypeCode]").append("<option value='"+this.foodTypeCode1+"' selected>"+this.foodTypeName+"</option>")
                            }else {
                                $("select[name=foodTypeCode]").append("<option value='"+this.foodTypeCode1+"'>"+this.foodTypeName+"</option>")
                            }
                        });
                        // 重新渲染
                        form.render('select');
                    }
                }
            });
        });


    </script>

  </body>

</html>